package yxy.game.pm2.active.command.bottlewish;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actwishsignup;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BottleWishMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/****************************************
 * @name CMD_Active_心愿瓶_BottleWishRegisterInfo
 * @description 心愿瓶活动签到页面信息
 * @author 梁健彬
 * @since 2022-08-22
 ***************************************/
@MessageType(proto = Command.BottleWishRegisterInfo, classtype = BottleWishMessage.BottleWishRegisterInfoRequest.class)
public class CMD_Active_心愿瓶_BottleWishRegisterInfo extends Command<BottleWishMessage.BottleWishRegisterInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, BottleWishMessage.BottleWishRegisterInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        BottleWishMessage.BottleWishRegisterInfoResponse.Builder response = BottleWishMessage.BottleWishRegisterInfoResponse.newBuilder();
        try {
            ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            Map<String, actwishsignup> allData = DataCenter.getAllData(actwishsignup.class);

            for (actwishsignup data : allData.values()) {
                BottleWishMessage.BottleWishItem.Builder builder = BottleWishMessage.BottleWishItem.newBuilder();
                builder.setDataId(data.getId());
                builder.setDay(data.getDays());
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                builder.addAllItem(items);//物品
                builder.setDisplay(data.getDisplay());
                response.addItem(builder);
            }

            response.setLogindays(am.getDay());//当前登录的天数
            response.addAllDrawDay(am.getHasRewardDays());//领取的天数
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    private void send(MessageSource messageSource, BottleWishMessage.BottleWishRegisterInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
